<?php

namespace App\Containers\AppSection\Project\UI\API\Transformers;

use App\Containers\AppSection\BankDetail\UI\API\Transformers\BankDetailTransformer;
use App\Containers\AppSection\File\UI\API\Transformers\FileTransformer;
use App\Containers\AppSection\Moderation\UI\API\Transformers\ModerationTransformer;
use App\Containers\AppSection\Pledge\UI\API\Transformers\PledgeTransformer;
use App\Containers\AppSection\Profile\UI\API\Transformers\ProfileTransformer;
use App\Containers\AppSection\Project\Models\Project;
use App\Containers\AppSection\Status\UI\API\Transformers\StatusTransformer;
use App\Containers\AppSection\Type\UI\API\Transformers\TypeTransformer;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class ProjectTransformer extends Transformer
{
    protected array $defaultIncludes = [
        'stage',
        'moderation_status'
    ];

    /**
     * @var  array
     */
    protected array $availableIncludes = [
        'company',
        'condition',
        'documents',
        'medias',
        'pledges',
        'bank_detail',
        'project_rating',
        'profile_rating',
        'document_rating',
        'rating',
        'moderation',
        'moderations',
        'profile',
        'analytic',
        'government',
    ];

    public function transform(Project $t): array
    {
        return [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'title' => $t->title,
            'description' => $t->description,
            'is_draft' => $t->is_draft,
            'stage_key' => $t->stage_key,
            'moderation_status_key' => $t->moderation_status_key,
            'moderated_at' => $this->dateFormatter($t->moderated_at),
            'businesses' => $t->businesses,
            'rate' => $t->rate,
            'videos' => $t->videos,
            'created_at' => $this->dateFormatter($t->created_at),
            'updated_at' => $this->dateFormatter($t->updated_at),
            'readable_created_at' => $t->created_at->diffForHumans(),
            'readable_updated_at' => $t->updated_at->diffForHumans()];
    }

    public function includeProfile(Project $t): Item
    {
        return $this->item($t->profile, new ProfileTransformer);
    }

    public function includeStage(Project $t): Item
    {
        return $this->item($t->stage, new TypeTransformer);
    }

    public function includeModerationStatus(Project $t): Item
    {
        return $this->item($t->moderation_status, new StatusTransformer);
    }

    public function includeCompany(Project $t): Item
    {
        return $this->item($t->company, new CompanyTransformer);
    }

    public function includeCondition(Project $t): Item
    {
        return $this->item($t->condition, new ConditionTransformer);
    }

    public function includeDocuments(Project $t): Collection
    {
        return $this->collection($t->documents, new DocumentTransformer);
    }

    public function includeMedias(Project $t): Collection
    {
        return $this->collection($t->medias()->get(), new FileTransformer);
    }

    public function includePledges(Project $t): Collection
    {
        return $this->collection($t->pledges, new PledgeTransformer);
    }

    public function includeBankDetail(Project $t): ?Item
    {
        return $this->nullableItem($t->profile->bank_details()
            ->where('master_account', true)
            ->first(), new BankDetailTransformer);
    }

    public function includeProjectRating(Project $t): Item
    {
        return $this->item($t->project_rating, new TypeTransformer);
    }

    public function includeProfileRating(Project $t): Item
    {
        return $this->item($t->profile_rating, new TypeTransformer);
    }

    public function includeDocumentRating(Project $t): Item
    {
        return $this->item($t->document_rating, new TypeTransformer);
    }

    public function includeRating(Project $t): Item
    {
        return $this->item($t->rating, new TypeTransformer);
    }

    public function includeModeration(Project $t): ?Item
    {
        return $this->nullableItem($t->moderation, new ModerationTransformer);
    }

    public function includeAnalytic(Project $t): Item
    {
        return $this->item($t->analytic, new AnalyticTransformer);
    }

    public function includeModerations(Project $t): Collection
    {
        return $this->collection($t->moderations, new ModerationTransformer);
    }

    public function includeGovernment(Project $t): ?Item
    {
        return $this->nullableItem($t->government, new GovernmentTransformer);
    }
}
